using System.Net;

namespace Fusion.Net.UDP
{
    public class UDPNet : NetBase
    {
        public void Send(string ip, int port, byte[] data, int length = -1)
        {
            if (socket == null)
            {
                return;
            }

            IPAddress ipAddress = IPAddress.Parse(ip);
            if (socket.Socket.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
            {
                ipAddress = ipAddress.MapToIPv6();
            }

            IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, port);
            Send(ipEndPoint, data, length);
        }

        public void Send(EndPoint ip, byte[] data, int length = -1)
        {
            if (socket == null)
            {
                return;
            }
            UDPSocket udp = (UDPSocket)socket;
            udp.TargetIP = ip;
            udp.Send(data, length > 0 ? length : data.Length);
        }
    }
}

